home *** CD-ROM | disk | FTP | other *** search
- Path: newsserver.amsinc.com!usenet
- From: sherborn@mail.amsinc.com (Steve Herborn)
- Newsgroups: comp.lang.c
- Subject: Re: need for function prototypes
- Date: Tue, 05 Mar 1996 19:10:44 GMT
- Organization: American Management Systems
- Message-ID: <4hi462$3bf@ams.amsinc.com>
- References: <4gutho$o1a@mn5.swip.net> <4hgbo0$2fj@s02.pavilion.co.uk>
- NNTP-Posting-Host: victor.amsinc.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- AJRobb@pavilion.co.uk (Andy J Robb) wrote:
-
- >chris.rossall@mailbox.swipnet.se (Chris Rossall) wrote:
-
- >>Hello I am having trouble understanding why I should use function
- >>prototypes. I mean,if the function is defined before it is used,the
- >>compiler should have enough information about the parameters to
- >>produce correct code anyway.
-
- >>-Chris
-
- >-----BEGIN PGP SIGNED MESSAGE-----
-
- >True, a function definition before a function call will serve as its
- >own declaration especially if it is restricted to file scope (static).
- >A lot of programs are written top-down. This leaves the function
- >definition after the function call. To get around this, insert a
- >function declaration before the function call.
-
- >Function prototypes come in to their own when defining functions with
- >external linkage. The declaration of these functions can then be
- >included in other files where the function is called. Also, including
- >the header file in the function source file allows the declaration to
- >be checked against the definition.
-
- >Regards,
- >Andy Robb.
-
- >-----BEGIN PGP SIGNATURE-----
- >Version: 2.6.2i
-
- >iQCVAwUBMTuyNpPl4P16x9sNAQFVIQQAvrKwK4vAjiGNYlP9olcWw6HkcJotYOhR
- >PDtvkY5d6ZWKCiv/7zrpI3Z/rAh5ThbSqzSQxdu7pM+rwQJf98D+lTXv+pD4m1Yr
- >zgr2+b88j5ttawySeQuzfOOCItb/jK5nSFMHQ1UO2o2Y8o3ELrCgG06tMAcmPaKW
- >DCwdsVWMpIw=
- >=dAKB
- >-----END PGP SIGNATURE-----
-
- >-----BEGIN PGP PUBLIC KEY BLOCK-----
- >Version: 2.6.2i
-
- >mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
- >MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
- >B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
- >tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
- >IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
- >=/wVD
- >-----END PGP PUBLIC KEY BLOCK-----
-
- The major advantage is that the data types of a function's arguments
- are clearly specified at the begining of a program. A common error in
- K&R programs was to call a function using the wrong data type for an
- argument; int, for example instead of long. This could lead to
- program failure that was difficult to debug, because there was no
- warning from the compiler. When a prototype is used, however, the
- compiler knows what data types to expect as arguments for the function
- and is always able to flag a mismatch as an error. Also, as in
- declaring variables the prototype clarifies for the programmer (or
- anyone else) looking at the source listing what each function is and
- what its arguments should be.
-
- The ANSI standard introduced type void for functions that dodn't
- return anything. Previously, a function that didn't return anything
- was considered to be type int by default, an inconsistent and
- potenially confusing approach.
-
- ANSI also introduced void as meaning that the function takes no
- arguments, thus removing another source of confusion.
-
- Steve Herborn
- American Management Systems Inc
-
- My opinions are my opinions. However, the above is quoted from Robert
- Lafore's C Programming Book.
-
-